home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0039_DPMI Memory in WinAPI.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  4KB  |  152 lines

  1. {
  2. > Protected mode has the WinAPI unit that lets you deal with
  3. > huge memory blocks and other stuff. That is what is needed.
  4.  
  5. > In real mode all you can do is:
  6.  
  7. Here's some stuff from a huge memory block unit I'm working on.  It isn't fully
  8. debugged yet, but I think these parts work.  However, use at your own risk.
  9. There are a few routines called which I don't include; you should be able to
  10. figure those ones out, or pull them out of a standard library.  "LH" is a
  11. record with fields L and H for pulling the low and high words out of a pointer
  12. or longint.
  13.  
  14.  { This part works in both real and protected mode. }
  15.  
  16.  procedure IncPtr(var p:pointer;count:word);
  17.  { Increments pointer }
  18.  begin
  19.    inc(LH(p).L,count);
  20.    if LH(p).L < count then
  21.      inc(LH(p).H,SelectorInc);
  22.  end;
  23.  
  24.  procedure DecPtr(var p:pointer;count:word);
  25.  { decrements pointer }
  26.  begin
  27.    if count > LH(p).L then
  28.      dec(LH(p).H,SelectorInc);
  29.    dec(LH(p).L,Count);
  30.  end;
  31.  
  32.  procedure IncPtrLong(var p:pointer;count:longint);
  33.  { Increments pointer; assumes count > 0 }
  34.  begin
  35.    inc(LH(p).H,SelectorInc*LH(count).H);
  36.    inc(LH(p).L,LH(Count).L);
  37.    if LH(p).L < LH(count).L then
  38.      inc(LH(p).H,SelectorInc);
  39.  end;
  40.  
  41.  procedure DecPtrLong(var p:pointer;count:longint);
  42.  { Decrements pointer; assumes count > 0 }
  43.  begin
  44.    if LH(count).L > LH(p).L then
  45.      dec(LH(p).H,SelectorInc);
  46.    dec(LH(p).L,LH(Count).L);
  47.    dec(LH(p).H,SelectorInc*LH(Count).H);
  48.  end;
  49.  { The next section is for real mode only }
  50.  
  51. {$ifndef dpmi}
  52.  
  53.  type
  54.    PFreeRec = ^TFreeRec;
  55.    TFreeRec = record
  56.      next: PFreeRec;
  57.      size: Pointer;
  58.    end;
  59.  
  60.  procedure GetMemHuge(var p:HugePtr;size:Longint);
  61.  const
  62.    blocksize = $FFF0;
  63.  var
  64.    prev,free : PFreeRec;
  65.    save,temp : pointer;
  66.    block : word;
  67.  begin
  68.    { Handle the easy cases first }
  69.    if size > maxavail then
  70.      p := nil
  71.    else if size < 65521 then
  72.      getmem(p,size)
  73.    else
  74.    begin
  75.  {$ifndef ver60}
  76.     {$ifndef ver70}
  77.      The code below is extremely version specific to the TP 6/7 heap manager!!
  78.     {$endif}
  79.  {$endif}
  80.      { Find the block that has enough space }
  81.      prev := PFreeRec(@freeList);
  82.      free := prev^.next;
  83.      while (free <> heapptr) and (PtrToLong(free^.size) < size) do
  84.      begin
  85.        prev := free;
  86.        free := prev^.next;
  87.      end;
  88.  
  89.      { Now free points to a region with enough space; make it the first one and
  90.        multiple allocations will be contiguous. }
  91.  
  92.      save := freelist;
  93.      freelist := free;
  94.      { In TP 6, this works; check against other heap managers }
  95.      while size > 0 do
  96.      begin
  97.        block := minlong(blocksize,size);
  98.        dec(size,block);
  99.        getmem(temp,block);
  100.      end;
  101.  
  102.      { We've got what we want now; just sort things out and restore the
  103.        free list to normal }
  104.  
  105.      p := free;
  106.      if prev^.next <> freelist then
  107.      begin
  108.        prev^.next := freelist;
  109.        freelist := save;
  110.      end;
  111.    end;
  112.  end;
  113.  
  114.  procedure FreeMemHuge(var p:HugePtr;size : longint);
  115.  const
  116.    blocksize = $FFF0;
  117.  var
  118.    block : word;
  119.  begin
  120.    while size > 0 do
  121.    begin
  122.      block := minlong(blocksize,size);
  123.      dec(size,block);
  124.      freemem(p,block);
  125.      IncPtr(p,block);
  126.      p := Normalized(p);
  127.    end;
  128.  end;
  129.  
  130. { The next section is the protected mode part }
  131.  
  132.  {$else}
  133.  
  134.  Procedure GetMemHuge(var p : HugePtr; Size: LongInt);
  135.  begin
  136.    if Size < 65521 then
  137.      GetMem(p,size)
  138.    else
  139.      p := GlobalAllocPtr(gmem_moveable,Size);
  140.  end;
  141.  
  142.  Procedure FreeMemHuge(var p : HugePtr; Size: Longint);
  143.  var
  144.    h : THandle;
  145.  begin
  146.    if Size < 65521 then
  147.      Freemem(p,size)
  148.    else
  149.      h := GlobalFreePtr(p);
  150.  end;
  151.  
  152.